; DeclsInfo:Tree Nodes=base-class for Hierarchy

;NewNode % ( obj$ , parent_ptr ) : "_NewNode@8"
;CopyNode % ( Source_ptr, Parent_ptr ) : "_CopyNode@8"
;DeleteNode ( node_ptr ) : "_DeleteNode@4"
;CountNodes % ( node_ptr ) : "_CountNodes@4"
;NodeObject $ ( node_ptr ) : "_NodeObject@4"
;SetNode ( node_ptr, pObject$  ) : "_SetNode@8"
;AfterNode % ( node_ptr ) : "_AfterNode@4"
;BeforeNode % ( node_ptr ) : "_BeforeNode@4"
;NodeFirst % ( node_ptr ) : "_NodeFirst@4"
;NodeLast % ( node_ptr ) : "_NodeLast@4"
;GetNode % ( node_ptr, id% ) : "_GetNode@8"
;NodeFind % ( node_ptr, find$  ) : "_NodeFind@8"
;NodeParent % ( node_ptr ) : "_NodeParent@4"
;ParentNode % ( node_ptr, parent_ptr ) : "_ParentNode@8"





Graphics 800,600,0,2
SetBuffer BackBuffer()



; Sample 1
Type tObj Field name$ End Type

Function NewObj(name$):Local o.tObj = New tObj:o\name=name:Return Handle(o):End Function
Function tObj.tObj(obj):Return Object.tObj(obj):End Function
Function ObjName$(obj):Local o.tObj=tObj(obj):Return o\name:End Function

Function PrintHierarchy(node, tab$="")
	Print tab+"| - '"+ObjName(NodeObject(node))+"'"
	Local nb=CountNodes(node), n
	Local child=NodeFirst(node)
	While child
		PrintHierarchy (child, tab+"|   ")
		child = AfterNode(child)
	Wend
End Function

Local Root			=	NewNode	( NewObj("Root"), 0 )
	Local child1	=	NewNode	( NewObj("Child 1"), Root )
	Local Hnd2		=	NewObj("Child 2")
	Local child2	=	NewNode	( Hnd2, Root )
	Local child3	=	NewNode	( NewObj("Child 3"), Root )
		Local sub21	=	NewNode	( NewObj("sub 21"), child2 )
		Local sub22	=	NewNode	( NewObj("sub 22"), child2 )


	Print "- name of child 2 -"
	Local obj = NodeFind(Root,Hnd2)
	Print "   * "+ObjName(NodeObject(obj))
	Print ""

	Print "- Hierarchy -
	PrintHierarchy(Root, "   ")
	Print ""

Print "- Hierarchy after removing child 2 -
	DeleteNode child2
	PrintHierarchy(Root, "   ")
	Print ""

FlushKeys:WaitKey:FlushKeys:Cls:Locate 0,0



; Sample 2
Function PrintPoints(Node, x=0,y=0)
	Local p = NodeObject(Node)
	x=x+V2X(p) : y=y+V2Y(p)
	
	Color 0,128,255
	Rect x-3, y-3, 7,7,0
	
	Local child = NodeFirst(Node)
	While child
		Local p2 = NodeObject(child)
		Color 255,255,000
		Line x,y, x+V2X(p2),y+V2Y(p2)
		
		PrintPoints(child, x, y)
		
		child=AfterNode(child)
	Wend
End Function

Root                                = NewNode ( NewV2 ( +400, +300 ), 0 )
    Local Col1                      = NewNode ( NewV2 ( +000, -100 ), Root )
        Local Col2                  = NewNode ( NewV2 ( +000, -100 ), Col1 )
            Local ShoulderL         = NewNode ( NewV2 ( -050, +000 ), Col2 )
			    Local ArmL          = NewNode ( NewV2 ( -080, +040 ), ShoulderL )
			        Local ForArmL   = NewNode ( NewV2 ( -090, +000 ), ArmL )
			            Local handL = NewNode ( NewV2 ( -040, +000 ), ForArmL )
					
            Local ShoulderR         = NewNode ( NewV2 ( +050, +000 ), Col2 )
			    Local ArmR          = NewNode ( NewV2 ( +080, +040 ), ShoulderR )
			        Local ForArmR   = NewNode ( NewV2 ( +090, +000 ), ArmR )
			            Local handR = NewNode ( NewV2 ( +040, +000 ), ForArmR )
			
            Local Neck              = NewNode ( NewV2 ( +000, -020 ), Col2 )
			
    Local HipL                      = NewNode ( NewV2 ( -050, +020 ), Root )
        Local KneeL                 = NewNode ( NewV2 ( +000, +100 ), HipL )
            Local HankleL           = NewNode ( NewV2 ( +000, +100 ), KneeL )
                Local FootL         = NewNode ( NewV2 ( +000, +030 ), HankleL )
    Local HipR                      = NewNode ( NewV2 ( +050, +020 ), Root )
        Local KneeR                 = NewNode ( NewV2 ( +000, +100 ), HipR )
            Local HankleR           = NewNode ( NewV2 ( +000, +100 ), KneeR )
                Local FootR         = NewNode ( NewV2 ( +000, +030 ), HankleR )

PrintPoints(Root)
Flip True

WaitKey
End


;~IDEal Editor Parameters:
;~C#Blitz3D